package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.VehicleMapper;
import cn.tedu.egcar1.pojo.dto.VehicleAddParam;
import cn.tedu.egcar1.pojo.dto.VehicleListQuery;
import cn.tedu.egcar1.pojo.entity.Vehicle;
import cn.tedu.egcar1.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name = "02-车辆模块", description = "20")
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam){
        log.debug("vehicleAddParam=" + vehicleAddParam );
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());

        log.debug("开始添加车辆对象：" + vehicle);
        int rows = vehicleMapper.insertVehicle(vehicle);
        if(rows >0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery) {
        log.debug("vehicleListQuery = " + vehicleListQuery);
        List<VehicleListVO> vehicleListVOS = vehicleMapper.selectVehicleList(vehicleListQuery);
        return JsonResult.ok(vehicleListVOS);
    }

    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@PathVariable Long id){
        log.debug("id = " + id);
        int rows = vehicleMapper.deleteVehicle(id);
        if (rows > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }

}
